package com.compass.mobile.ui.activity

import LogUtil
import UserInfoManager
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import androidx.leanback.widget.OnChildViewHolderSelectedListener
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.compass.mobile.R
import com.compass.mobile.databinding.ActivityMyOrderBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Dictionaries
import com.compass.mobile.presenters.DictionariesPresenter
import com.compass.mobile.ui.fragment.OrderFragment
import com.compass.mobile.widget.MyBridgeAdapter
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import toBeanOrNull
import toJson

/**
 * Description： 我的订单（问诊订单）
 * Date:2024/6/19 9:12
 */
class MyOrderActivity : BaseVBActivity<ActivityMyOrderBinding>() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, MyOrderActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var list: MutableList<Dictionaries>? = null
    private lateinit var orderFragment: OrderFragment
    override fun initView(savedInstanceState: Bundle?) {
        orderFragment = OrderFragment()
        displaySelectedFragment(orderFragment)
    }

    override fun initData() {
        list = UserInfoManager.getOrderTypeSource()?.toBeanOrNull<MutableList<Dictionaries>>()
        list?.add(0, Dictionaries("全部",""))
        LogUtil.d("list==${list?.toJson()}")
        initCaseTitle(list)
    }


    // 根据需要显示或替换Fragment
    private fun displaySelectedFragment(fragment: Fragment) {
        val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentChild, fragment)
        fragmentTransaction.commit()
    }

    @SuppressLint("RestrictedApi")
    fun initCaseTitle(dictionaries: MutableList<Dictionaries>?) {
        mBinding.gridCase.verticalSpacing = dpToPx(10)
        val arrayObjectAdapter = ArrayObjectAdapter(DictionariesPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            this,
            arrayObjectAdapter, null
        )
        mBinding.gridCase.setAdapter(itemBridgeAdapter)
        arrayObjectAdapter.addAll(0, dictionaries)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )
        mBinding.gridCase.setOnChildViewHolderSelectedListener(onChildViewHolderSelectedListener)
        lifecycleScope.launch {
            delay(500)
            //延迟获取焦点
            mBinding.gridCase.requestFocus()
        }

    }


    // 保存上一个选中的位置 实现高亮显示效果
    var lastSelectedPosition: Int = -1
    private val onChildViewHolderSelectedListener: OnChildViewHolderSelectedListener = object :
        OnChildViewHolderSelectedListener() {
        override fun onChildViewHolderSelected(
            parent: RecyclerView?,
            childViewHolder: RecyclerView.ViewHolder?,
            position: Int,
            subposition: Int
        ) {
            childViewHolder?.itemView?.setBackgroundResource(R.drawable.case_focus)
            if (lastSelectedPosition!=-1){
                var lastViewHolder=parent?.findViewHolderForAdapterPosition(lastSelectedPosition)
                lastViewHolder?.itemView?.setBackgroundResource(R.drawable.case_title_selector)
            }
            lastSelectedPosition=position
            val item = list?.get(position)
            item?.let {
                orderFragment.setSelect(it.value)
            }

        }
    }


}
